#include <Wire.h>
#include <Adafruit_SGP30.h>
#include <SoftwareSerial.h>
#include "Metro.h"
#include "OLED.h"
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
extern U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2;
oled_bzy oled;
SoftwareSerial mySerial(2, 3); //(2（RX）,3(TX))接ESP8266的（TX,RX） 软串口
Metro blink1Metro = Metro(150);
Metro blink2Metro = Metro(150); //两个线程
//定义全局变量
int h = 0 , t = 0;
int vale = 0 , dlx = 0 , air = 0;
char datatmp[15]; // 网络发送的数据
//截至
Adafruit_SGP30 sgp; //tvoc的对象
void setup() {
  Wire.begin();
  oled.init_oled();
  oled.clear_of_oled();
  Serial.begin(9600);
  Serial.println("SGP30 test");
  esp_init();
  dht.begin();
  if (! sgp.begin()) {
    Serial.println("Sensor not found :(");
    while (1);
  }
}
//初始化esp01
void esp_init()
{
  mySerial.begin(115200);
  mySerial.println("AT+CWMODE=2\r");//向8266发送的指令  //模式2 esp01的三个模式
  delay(1000);
  mySerial.println("AT+CIPMUX=1\r");//开启多连接模式
  delay(1000);
  mySerial.println("AT+CIPSERVER=1,8080\r");  //192.168.4.1 8080
  delay(1000);
}
//初始化OLED
void oled_show()
{
  if (! sgp.IAQmeasure()) {
    Serial.println("Measurement failed");
    return;
  }
  vale = sgp.TVOC;
  dlx = sgp.eCO2; //获取数据
  t = dht.readTemperature();  //读取温度
  air = analogRead(A0);       //读取nh3
  air = map(air , 0 , 1024 , 0 , 100);  //adc
  h = dht.readHumidity();     //读取湿度
  u8g2.firstPage();
  do {          //128x64
    oled.delay_text(0, 10, "CO2: ");
    oled.delay_number(50, 10, dlx);
    oled.delay_text(100, 10, "ppm");

    oled.delay_text(0, 21, "TVOC: ");
    oled.delay_number(50, 21, vale);
    oled.delay_text(100, 21, "ppb");

    oled.delay_text(0, 32, "NH3: ");
    oled.delay_number(50, 32, air);
    oled.delay_text(100, 32, "%");

    oled.delay_text(0, 43, "h: ");
    oled.delay_number(50, 43, h);
    oled.delay_text(100, 43, "%");

    oled.delay_text(0, 54, "t: ");
    oled.delay_number(50, 54, t);
    oled.delay_text(100, 54, "C");
  } while ( u8g2.nextPage());
}
//获取温湿度
void get_dht()
{
  h = dht.readHumidity();
  t = dht.readTemperature();
}
//获取气体
void get_air()
{
  if (! sgp.IAQmeasure()) {
    Serial.println("Measurement failed");
    return;
  }
  vale = sgp.TVOC;
  delay(50);
  dlx = sgp.eCO2;
  delay(50);
  vale = map(vale , 0 , 60000 , 0 , 100);
  dlx = map(dlx , 400 , 60000 , 0 , 100);
}
//esp01发送
void esp01s_write()
{
  memset(datatmp,"\0",14);
  get_air();
  air = analogRead(A0);
  air = map(air , 0 , 1024 , 0 , 100);
  datacheck1(h , true , false);
  datacheck1(t , false , false);
  datacheck1(air , false , true);
  datacheck2(vale , true);
  datacheck2(dlx , false);
  //Serial.println(datatmp);
  senddata(datatmp, 15);
}
void senddata(char *Data , int len)
{
  char *da = "";
  mySerial.println("AT+CIPSEND=0,14\r");
  for (int i = 0; i < 15; i++)
  {
    mySerial.print(*(Data));
    delay(100);
    Data++;
  }
  da = "";
}
//数据处理
void datacheck1(int number , bool b , bool c)
{
  int k = number / 10;
  int m = number % 10;
  if (b)
  {
    datatmp[7] = k + '0';
    datatmp[8] = m + '0';
    datatmp[9] = ',';
  }
  else if (!b && !c)
  {
    datatmp[10] = k + '0';
    datatmp[11] = m + '0';
    datatmp[12] = ',';
    //datatmp[18] = '\r';
  }
  else
  {
    datatmp[13] = k + '0';
    datatmp[14] = m + '0';
  }
}
void datacheck2(int number , bool b)
{
  int i = number / 10;
  int j = number % 10;
  if (b)
  {

    datatmp[0] = '#';
    datatmp[1] = i + '0';
    datatmp[2] = j + '0';
    datatmp[3] = ',';
  }
  else
  {
    datatmp[4] = i + '0';
    datatmp[5] = j + '0';
    datatmp[6] = ',';
  }
}
void loop() {
  if (blink1Metro.check())
  {
    oled_show();          //oled显示
  }
  if (blink2Metro.check())
  {
    esp01s_write();     //esp发送
  }
}
